﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Acme.BookStore.IReservationss;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.IReservations
{
    /// <summary>
    /// 酒店服务
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class ReservationsServices : ApplicationService, IReservationsServices
    {
        IRepository<Reservations> repository;
        IRepository<RoomType> GetRepository;
        IRepository<RoomNumber> GrtRoomNumber;
        IRepository<StoreManagement> IReservationsServices;

        public ReservationsServices(IRepository<Reservations> repository, IRepository<RoomType> getRepository, IRepository<RoomNumber> grtRoomNumber, IRepository<StoreManagement> iReservationsServices)
        {
            this.repository = repository;
            GetRepository = getRepository;
            GrtRoomNumber = grtRoomNumber;
            IReservationsServices = iReservationsServices;
        }






        /// <summary>
        /// 酒店的反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetReservationByIdAsync")]
        public async Task<ApiResult> GetReservationByIdAsync(Guid Id)
        {
            var result = await IReservationsServices.FirstOrDefaultAsync(x => x.Id == Id);
            return ApiResult.Ok(result);
        }
        /// <summary>
        /// 房型
        /// </summary>
        /// <returns></returns>
        [HttpGet("GrtRomType")]
        public async Task<ApiResult> GrtRomType()
        {
            var result = (await GetRepository.GetListAsync()).ToList();
            List<RomTypeDTO> dto = new List<RomTypeDTO>();
            foreach (var item in result)
            {
                dto.Add(new RomTypeDTO
                {
                    Id = item.Id,
                    TypeName = item.RoomName
                }
                    );
            }

            return ApiResult.Ok(dto);
        }
        [HttpGet("GetRoomNumber")]
        public async Task<ApiResult> GetRoomNumber(Guid RoomTypeId)
        {
            var result = await GrtRoomNumber.FirstOrDefaultAsync(x => x.RoomTypeId == RoomTypeId);
            List<RoomNumberDTO> roomNumberDTOs= new List<RoomNumberDTO>();
            roomNumberDTOs.Add(new RoomNumberDTO
            {
                RoomNO = result.RoomNO,
                Id = result.Id
            });
            return ApiResult.Ok(roomNumberDTOs);
        }
        /// <summary>
        /// 预订房间
        /// </summary>
        /// <param name="reservationDto"></param>
        /// <returns></returns>
        [HttpPost("InsertReservationAsync")]
        public async Task<ApiResult> InsertReservationAsync(InsertReservationsDTO reservationDto)
        {
            var entity = ObjectMapper.Map<InsertReservationsDTO, Reservations>(reservationDto);
            await repository.InsertAsync(entity);
            return ApiResult.Ok();
        }
    }
}
